package com.wxy.xr.servlet;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.wxy.xr.bean.Contact;
import com.wxy.xr.bean.ContactListParam;
import com.wxy.xr.service.ContactService;
import com.wxy.xr.service.UserService;
import com.wxy.xr.service.WebsiteService;
import com.wxy.xr.service.impl.UserServiceImpl;
import com.wxy.xr.service.impl.WebsiteServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/contact/*")
public class ContactServlet extends BaseServlet<Contact>{
    private UserService userService=new UserServiceImpl();
    private WebsiteService websiteService=new WebsiteServiceImpl();
    public  void front(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setAttribute("user",userService.list().get(0));
        request.setAttribute("footer",websiteService.list().get(0).getFooter());
        forward(request, response,"front/contact.jsp");
    }
    public  void admin(HttpServletRequest request, HttpServletResponse response) throws Exception{
        ContactListParam param=new ContactListParam();
        BeanUtils.populate(param,request.getParameterMap());
        request.setAttribute("result", ((ContactService) service).list(param));
        forward(request, response,"admin/contact.jsp");
    }

    public  void save(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //检查验证码
        String code= (String) request.getSession().getAttribute("code");
        String captcha=request.getParameter("captcha");
        if(!code.equals(captcha)){
            forward(request, response,"验证码错误");
        }
        Contact contact=new Contact();
        BeanUtils.populate(contact,request.getParameterMap());
        if(service.save(contact)){
            redirect(request, response,"contact/front");
        } else {
            forward(request, response,"留言信息保存失败");
        }
    }
    public void read(HttpServletRequest request, HttpServletResponse response) throws Exception{
        Integer id = Integer.valueOf(request.getParameter("id"));
        Map<String,Object> result = new HashMap<>();
               if(((ContactService) service).read(id)) {
                   result.put("success","true");
                   result.put("msg","更新成功");
               }else{
                   result.put("success","false");
                   result.put("msg","更新失败");
               }
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(new ObjectMapper().writeValueAsString(result));
    }
}
